package game;

import java.net.*;
import java.io.*;
/*
 * class that is used to communication with single player
 */
public class BadugiClient extends Thread implements Runnable {
	private BadugiServer s;
	public boolean slotready;
	private int ID;
	public Socket socket;
	public PrintWriter writer;
	public BufferedReader reader;
	private String name;
	public Player player;
	
	BadugiClient(BadugiServer s,int ID) {
		this.s=s;
		this.ID=ID;
		slotready=true;
		player=new Player();
	}
	
	@Override
	public void run() {
		while(true) {
			if(!(slotready)) {
				System.out.println("player "+ID+" - "+name+" in");
				String input="";
				try {
					input=reader.readLine();
				} catch (IOException ex) {
				}
				while (!(input.equals("Q"))) {
					System.out.println(s.gamelogic(ID,input));
					try {
						input=reader.readLine();
					} catch (IOException e) {
					}
				}
				writer.println(s.gamelogic(ID,"Q"));
				System.out.println("player "+ID+" - "+name+" out");
				slotready=true;
			} else {
				try {
					sleep(1);
				} catch (InterruptedException e) {
				}
			}
		}
	}
	
	public void setPlayerName(String newname) {
		name=newname;
	}
	
	public String getPlayerName() {
		if (slotready && player.bot==0) {
			return "";
		}
		return name;
	}
}